<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert
                type="warning"
                title="温馨提示：1.用户可以根据商品分类搜索商品，请正确创建分类；2.点击分类名称前符号，显示该商品分类的下级分类。"
                :closable="false"
                show-icon
            />
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-button type="primary" @click="handleAdd"> 添加分类 </el-button>
            <el-button @click="handleExpand"> 展开/折叠 </el-button>

            <el-table
                ref="tableRef"
                size="large"
                class="mt-[14px]"
                :data="pager.lists"
                v-loading="pager.loading"
                row-key="id"
                :tree-props="{ children: 'child_cats', hasChildren: 'hasChildren' }"
            >
                <el-table-column label="分类名称" prop="name" min-width="190" />
               <!-- <el-table-column label="分类图片" min-width="140">
                    <template #default="{ row }">
                        <div class="flex leading-[40px]">
                            <el-avatar :src="row.image" :size="40" />
                        </div>
                    </template>
                </el-table-column> -->
                <el-table-column label="状态" prop="status" min-width="150">
                    <template #default="{ row }">
                        <el-switch
                            v-model="row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @click="handleChange(row)"
                        />
                    </template>
                </el-table-column>
                <!-- <el-table-column label="商品数量" prop="goods_num" min-width="150" /> -->
                <el-table-column label="排序" prop="sort" min-width="150" />
                <el-table-column label="创建时间" prop="create_at" min-width="180" />
                <el-table-column label="操作" min-width="170" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['auth.admin/edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-if="row.root != 1"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <!-- <pagination v-model="pager" @change="getLists" /> -->
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import type { ElTable } from 'element-plus'
import { shopCatListDel, shopGoodslist,shopCatListQuickEdit} from '@/api/goods'
import EditPopup from './edit.vue'
import feedback from '@/utils/feedback'

const { pager, getLists } = usePaging({
    fetchFun: shopGoodslist
})
//表格展开
const tableRef = shallowRef<InstanceType<typeof ElTable>>()
let isExpand = false
const toggleExpand = (children: any[], unfold = true) => {
    for (const key in children) {
        tableRef.value?.toggleRowExpansion(children[key], unfold)
        if (children[key].children) {
            toggleExpand(children[key].children!, unfold)
        }
    }
}
const handleExpand = () => {
    isExpand = !isExpand
    toggleExpand(pager.lists, isExpand)
}
//删除数据
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await shopCatListDel({ id_str:id })
    getLists()
}

//初始获得数据
getLists()

//展开edit
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

//改变状态
const handleChange = async (params: any) => {
	params.field_name = "status"
	params.field_val = params.status
    await shopCatListQuickEdit(params)
}
</script>

<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}
</style>
